package org.forwhat.dao.Impl;

import org.forwhat.bean.WhatPost;
import org.forwhat.dao.WhatPostDao;
import org.forwhat.framework.annotation.Data;
import org.forwhat.framework.annotation.InsertBean;

import java.util.List;

/**
 * @projectName: ForWhat
 * @package: org.forwhat.dao.Impl
 * @className: WhatPostDaoImpl
 * @author: LGJ
 * @description: TODO
 * @date: 2023/11/23 22:00
 * @version: 1.0
 */

//@Data("WhatPostDaoImpl")
public class WhatPostDaoImpl implements WhatPostDao {

    @InsertBean("BaseDaoImpl")
    private BaseDaoImpl baseDaoImpl;
    public BaseDaoImpl getBaseDaoImpl() {
        return baseDaoImpl;
    }

    public void setBaseDaoImpl(BaseDaoImpl baseDaoImpl) {
        this.baseDaoImpl = baseDaoImpl;
    }

    @Override
    public List<WhatPost> select(int start,int count,String search) {
        try {
            List<WhatPost> whatPosts;
            if (search!=null && !search.equals("")){
                whatPosts = baseDaoImpl.selectList(
                        "select post_id," +
                                "       post_user_id," +
                                "       post_state," +
                                "       post_title," +
                                "       post_read_count," +
                                "       post_community," +
                                "       post_create_time," +
                                "       post_update_time," +
                                "       (select user_name from what_user where what_user.user_id = post_operator) as post_operator, " +
                                "(select COUNT(*) from what_comment where comment_parent = what_post.post_id) as 'comment_count'" +
                                "from what_post where post_title like concat('%',?,'%')order by post_create_time desc limit ?,? ;", WhatPost.class, search, start, count);
            }else {
                whatPosts = baseDaoImpl.selectList(
                        "select post_id," +
                                "       post_user_id," +
                                "       post_state," +
                                "       post_title," +
                                "       post_read_count," +
                                "       post_community," +
                                "       post_create_time," +
                                "       post_update_time," +
                                "       (select user_name from what_user where what_user.user_id = post_operator) as post_operator, " +
                                "(select COUNT(*) from what_comment where comment_parent = what_post.post_id) as 'comment_count'" +
                                "from what_post order by post_create_time desc limit ?,? ;",WhatPost.class, start,count);
            }


            return whatPosts;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public List<WhatPost> selectHotPost(int length) {
        try {
            if (length <= 0) {
                length = 10;
            }
            return baseDaoImpl.selectList("select post_id,post_state,post_title from what_post order by post_read_count DESC limit ?", WhatPost.class, length);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public int addPostReadCount(String postId) {

        return baseDaoImpl.update("update what_post set post_read_count = post_read_count + 1 where post_id = ?", postId);

    }

    @Override
    public int insertPost(WhatPost whatPost) {

            return baseDaoImpl.update("insert into what_post( post_id,post_user_id, post_title, post_create_time,post_update_time,post_operator) value(?,?,?,?,?,?)",
                    whatPost.getPostId(),
                    whatPost.getPostUserId(),
                    whatPost.getPostTitle(),
                    whatPost.getPostCreateTime(),
                    whatPost.getPostUpdateTime(),
                    whatPost.getPostUserId());

    }

    @Override
    public String selectLatestPost() {

        try {
            return (String) baseDaoImpl.selectObject("select post_id from what_post order by post_create_time desc limit 1", null);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }

    @Override
    public WhatPost selectPostByPostId(String postId) {
        try {
            return baseDaoImpl.selectBean(
                        " select post_id," +
                            "       post_user_id," +
                            "       post_state," +
                            "       post_title," +
                            "       post_read_count," +
                            "       post_community," +
                            "       post_create_time," +
                            "       post_update_time 'post_update_time'," +
                            "       (select user_name from what_user where user_id = what_post.post_user_id) 'post_user_name'," +
                            "       (select COUNT(*) from what_comment where comment_parent = what_post.post_id) as 'comment_count'" +
                            "from what_post " +
                            "where what_post.post_id = ?",
                    WhatPost.class,
                    postId);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
